package com.mnsq.book.service.impl;

import com.mnsq.book.bean.condition.BookCondition;
import com.mnsq.book.bean.entity.Book;
import com.mnsq.book.dao.BookRepository;
import com.mnsq.book.service.BookService;
import com.mnsq.common.bean.dto.PageInfo;
import com.mnsq.common.tools.jpa.JpaExample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

/**
 * @author Administrator
 * @date 2019/1/21 0021
 */
@Slf4j
@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookRepository bookRepository;

    @Override
//    @AutoCache(value = 5, fixedValue = false, prefix = "book", cls = PageInfo.class)
    public PageInfo<Book> queryPageByCondition(BookCondition bc) {
        PageInfo<Book> info = new PageInfo<>();
        Page<Book> all;
        if (bc.isEmpty()) {
            all = bookRepository.findAll(PageRequest.of(bc.getPageNum(), bc.getPageSize()));
        } else {
            all = bookRepository.findAll(JpaExample.getInstance().getExample(bc, bc.getInstance()), PageRequest.of(bc.getPageNum(), bc.getPageSize()));
        }
        info.setContent(all.getContent()).setTotalPages(all.getTotalPages());
        return info;
    }
}
